package cn.demomaster.quickpicturer.picker;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import cn.demomaster.quickpicturer.config.ImageConfig;
import cn.demomaster.quickpicturer.config.ImageActionRequest;
import cn.demomaster.quickpicturer.constants.ImageActionType;
import cn.demomaster.quickpicturer.constants.ImageSourceType;
import cn.demomaster.quickpicturer.helper.PhotoHelper;


public class QuickPicturer {

    private QuickPicturer() {
        photoHelper = PhotoHelper.getInstance();
    }

    static QuickPicturer instance;
    public static QuickPicturer getInstance() {
        if (instance == null) {
            instance = new QuickPicturer();
        }
        return instance;
    }
//    public static QuickPicturer get(Activity activity) {
//        if (instance == null) {
//            instance = new QuickPicturer();
//        }
//        return instance;
//    }
//
//    public static QuickPicturer get(Fragment fragment) {
//        return get(fragment.getActivity());
//    }

    public void selectImages(ImageConfig config, OnImageSelectedListener listener) {
        if (config.getSources().length == 1) {
            handleSingleSource(config, 0, listener);
        } else {
            showSourceSelectionDialog(config, listener);
        }
    }

    public void onActivityResult(Context context, int requestCode, int resultCode, Intent data) {
        photoHelper.onActivityResult(context, requestCode, resultCode, data);
    }

    private PhotoHelper photoHelper;

    private void handleSingleSource(ImageConfig config, int index, OnImageSelectedListener listener) {
        ImageSourceType source = config.getSources()[index];
        OnImageSelectedListener onImageSelectedListener = new OnImageSelectedListener() {
            @Override
            public void onImagesSelected(Uri[] imagePath) {
                listener.onImagesSelected(imagePath);
            }
        };
        ImageActionRequest request = null;
        switch(source){
            case CAMERA:
                request = new ImageActionRequest(config, ImageActionType.TAKE_PHOTO);
                break;
            case CAMERA_CROP:
                request = new ImageActionRequest(config, ImageActionType.TAKE_PHOTO);
                request.setCroppingEnabled(true);
                break;
            case GALLERY:
                request = new ImageActionRequest(config, ImageActionType.SELECT_FROM_GALLERY);
                break;
            case GALLERY_CROP:
                request = new ImageActionRequest(config, ImageActionType.SELECT_FROM_GALLERY_AND_CROP);
                request.setCroppingEnabled(true);
                break;
            case CUSTOM_GALLERY:
                request = new ImageActionRequest(config, ImageActionType.SELECT_FROM_CUSTOM_GALLERY);
                break;
            case CUSTOM_GALLERY_CROP:
                request = new ImageActionRequest(config, ImageActionType.SELECT_FROM_CUSTOM_GALLERY_AND_CROP);
                request.setCroppingEnabled(true);
                break;
        }
        if(request!=null) {
            request.setOnImageSelectedListener(onImageSelectedListener);
            photoHelper.execute(request);
        }
//        switch (source) {
//            case CAMERA:
//
//                //takePhoto(config.outputUri, onImageSelectedListener);
//
//                photoHelper.execute(, onImageSelectedListener);
//                break;
//            case GALLERY:
//                //chooseFromGallery(onImageSelectedListener);
//                photoHelper.selectPhotoFromGallery(config,onImageSelectedListener);
//                break;
//            case CUSTOM_GALLERY:
//                photoHelper.selectPhotoFromMyGallery(config, onImageSelectedListener);
//                //chooseFromCustomGallery(config.getMaxImages(), onImageSelectedListener);
//                break;
//            case CAMERA_CROP:
//                //takePhotoAndCrop(config.outputUri, onImageSelectedListener);
//                photoHelper.takePhotoCrop(config, onImageSelectedListener);
//                break;
//            default:
//                throw new IllegalArgumentException("Unsupported source: " + source);
//        }
    }

    public void takePhoto(ImageConfig imageConfig, OnImageSelectedListener onImageSelectedListener) {
//        ImageRequestBuilder builder = new ImageRequestBuilder(mActivity);
//        builder.setOutUri(outUri);
//        builder.setRequestType(ImageSource.CAMERA);
//        builder.setOnImageSelectedListener(onImageSelectedListener);
        ImageActionRequest imageActionRequest = new ImageActionRequest(imageConfig,ImageActionType.TAKE_PHOTO);
        imageActionRequest.setOnImageSelectedListener(onImageSelectedListener);
        photoHelper.execute(imageActionRequest);
    }

    public void takePhotoAndCrop(Uri uri, OnImageSelectedListener onImageSelectedListener) {
        //photoHelper.takePhotoCrop(uri, onImageSelectedListener);
    }

    public void chooseFromCustomGallery(ImageConfig imageConfig,OnImageSelectedListener onImageSelectedListener) {
        //ImageConfigBuilder imageConfigBuilder =new ImageConfigBuilder(activity);
        //photoHelper.selectPhotoFromMyGallery(imageConfig, onImageSelectedListener);
        ImageActionRequest imageActionRequest = new ImageActionRequest(imageConfig,ImageActionType.SELECT_FROM_CUSTOM_GALLERY);
        imageActionRequest.setOnImageSelectedListener(onImageSelectedListener);
        photoHelper.execute(imageActionRequest);
    }

    public void chooseFromCustomGalleryAndCrop(ImageConfig imageConfig,OnImageSelectedListener onImageSelectedListener) {
        //ImageConfigBuilder imageConfigBuilder =new ImageConfigBuilder(activity);
        //photoHelper.selectPhotoFromMyGallery(imageConfig, onImageSelectedListener);
        ImageActionRequest imageActionRequest = new ImageActionRequest(imageConfig,ImageActionType.SELECT_FROM_CUSTOM_GALLERY_AND_CROP);
        imageActionRequest.setOnImageSelectedListener(onImageSelectedListener);
        imageActionRequest.setCroppingEnabled(true);
        photoHelper.execute(imageActionRequest);
    }

    public void chooseFromGallery(OnImageSelectedListener onImageSelectedListener) {
        //photoHelper.selectPhotoFromGallery(onImageSelectedListener);
    }

    private void showSourceSelectionDialog(ImageConfig config, OnImageSelectedListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(config.getActivity());
        builder.setTitle("选择图片");
        CharSequence[] charSequences = new CharSequence[config.getSources().length];
        if (config.getSourceNames() != null) {
            for (int i = 0; i < config.getSources().length; i++) {
                charSequences[i] = config.getSourceNames()[i]; // 或者使用 enums[i].toString()
            }
        } else {
            for (int i = 0; i < config.getSources().length; i++) {
                charSequences[i] = config.getSources()[i].name(); // 或者使用 enums[i].toString()
            }
        }
        builder.setItems(charSequences, (dialog, which) -> {
            //ImageSource selectedSource = config.getSources()[which];
            handleSingleSource(config, which, listener);
        });
        builder.show();
    }

    public interface OnImageSelectedListener {
        void onImagesSelected(Uri... images);
    }
}
